﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace IdentifyGuideLines
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StreamReader sr1 = new StreamReader(@"d:/jinrong_3.txt", Encoding.GetEncoding("gb2312"));
            string str1 = sr1.ReadToEnd();
            sr1.Close();
            StreamReader sr2 = new StreamReader(@"d:/feijin_3.txt", Encoding.GetEncoding("gb2312"));
            string str2 = sr2.ReadToEnd();
            sr2.Close();

            str1 = str1.Replace("\r\n", ",");
            List<string> jinGLs = new List<string>(str1.Split(','));
            List<string> jinGLs2 = new List<string>();
            foreach (string str in jinGLs)
            {
                jinGLs2.Add(str.Trim());
            }
            jinGLs.Clear();
            jinGLs.AddRange(jinGLs2);

            str2 = str2.Replace("\r\n", ",");
            List<string> feijinGLs = new List<string>(str2.Split(','));
            List<string> feijinGLs2 = new List<string>();
            foreach (string str in feijinGLs)
            {
                feijinGLs2.Add(str.Trim());
            }
            feijinGLs.Clear();
            feijinGLs.AddRange(feijinGLs2);


            //int index = 10;
            //int noIndex = 93001;
            //foreach (string feijinGL in feijinGLs)
            //{
            //    if (jinGLs.Contains(feijinGL))
            //    {
            //        InsertGL(feijinGL, index, -1, 2, noIndex, 93);
            //    }
            //    else
            //    {
            //        InsertGL(feijinGL, index, -1, 1, noIndex, 93);
            //    }
            //    index += 10;
            //    noIndex += 1;
            //}

            int baseOrder = 10;
            int baseNO = 93037;
            foreach (string jinGL in jinGLs)
            {
                int theIndex = feijinGLs.IndexOf(jinGL);
                if (theIndex == -1)
                {
                    InsertGL(jinGL, -1, baseOrder, 3, baseNO, 93);
                    baseNO += 1;
                }
                else
                {
                    UpdateGL(theIndex + 93001, baseOrder);
                }
                baseOrder += 10;
            }
      
        }

        public void InsertGL(string name, int? order1, int? order2,  int type, int no, int report)
        {
            string sql = "INSERT t_gls (name, [order1], [order2], type, [no], report) values (@name, @order1, @order2, @type, @no, @report)";
            System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection("server=(local);uid=sa;pwd=sa;database=JYStockAnalyser");
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, cn);
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", name));
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@order1", order1));
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@order2", order2));
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@type", type));
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@no", no));
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@report", report));
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
        }

        public void UpdateGL(int no, int order2)
        {
            string sql = "UPDATE t_gls SET order2 = @order2 WHERE [no] = @no";
            System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection("server=(local);uid=sa;pwd=sa;database=JYStockAnalyser");
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, cn);
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@no", no));
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@order2", order2));       
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            StreamReader sr1 = new StreamReader(@"d:/aa.txt", Encoding.GetEncoding("gb2312"));
            string str1 = sr1.ReadToEnd();
            sr1.Close();

            str1 = str1.Replace("\r\n", ",");
            List<string> jinGLs = new List<string>(str1.Split(','));

            for (int i = 0; i < jinGLs.Count; i++)
            {
                InsertGL(jinGLs[i].Trim(), i * 10, i * 10, 2, i + 10001, 10);
            }
        }
    }
}
